package com.wceManage.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wceManage.beans.page.PageList;
import com.wceManage.data.mapper.BaseMapper;
import com.wceManage.services.BaseService;

import java.util.List;

/**
 * BaseServiceImpl
 * 基础base实现
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    public abstract BaseMapper<T> getMapper();

    /**
     * 添加
     *
     * @param bean
     * @return
     */
    public int add(T bean) {
        return getMapper().add(bean);
    }

    /**
     * 更新
     *
     * @param bean
     * @return
     */
    public int update(T bean) {
        return getMapper().update(bean);
    }

    /**
     * 删除
     *
     * @param bean
     * @return
     */
    public int delete(T bean) {
        return getMapper().delete(bean);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public T queryById(long id) {
        return getMapper().queryById(id);
    }

    /**
     * 根据列表查询
     *
     * @param bean
     * @return
     */
    public List<T> queryList(T bean) {
        return getMapper().queryList(bean);
    }

    /**
     * 是否存在
     *
     * @param bean
     * @return
     */
    public int exists(T bean) {
        return getMapper().exists(bean);
    }

    /**
     * 查询所有数据列表
     *
     * @param bean
     * @return
     */
    public List<T> findAllList(T bean) {
        return getMapper().findAllList(bean);
    }

    /**
     * 查询所有数据列表
     *
     * @return
     * @see public List<T> findAllList(T entity)
     */
    @Deprecated
    public List<T> findAllList() {
        return getMapper().findAllList();
    }

    /**
     * 获取最大的排序值
     *
     * @return
     */
    public int getMaxSeq() {
        return getMapper().getMaxSeq();
    }

    /**
     * 分页查询
     *
     * @param pageList
     * @param bean
     * @return
     */
    public PageList<T> queryPage(PageList<T> pageList, T bean) {
        Page<T> page;
        int pageStart;
        if (pageList.getiDisplayLength() == -1) {//不分页
            pageStart = 1;
            page = PageHelper.startPage(pageStart, 10000, false);
        } else {
            pageStart = pageList.getiDisplayStart() / pageList.getiDisplayLength() + 1;
            page = PageHelper.startPage(pageStart, pageList.getiDisplayLength());
        }
        getMapper().queryList(bean);
        pageList.setiTotalRecords(page.getTotal());
        pageList.setiTotalDisplayRecords(pageList.getiTotalRecords());
        pageList.setAaData(page.getResult());
        return pageList;
    }
}
